#include <string.h>
#include <windows.h>

#include "lib.h"
#include "patch.h"
#include "admrm.h"
#include "admpipe.h"

#include "patchsrv.h"
#include "pkglist.h"
#include "pkgdesc.h"

void
AdminRemovePkg::handle(uint8* msg, int32 len)
{
	if (pipe->isLoggedOn())
	{
		uint32 which;
		PackageDescription* pkg;
		PackageList* list;

		which = (uint32)(msg[1] & 0xFF);

		list = PatchServer::This->lockPackageList();
		if (!list)
		{
			PatchServer::This->unlockPackageList();
			pipe->sendError(APE_MEMORY_ERROR);
			return;
		}

		pkg = list->getItem(which);
		if (!pkg)
		{
			PatchServer::This->unlockPackageList();
			pipe->sendError(APE_UNKNOWN_PACKAGE);
			return;
		}

//		if (0 == strcmpi(pkg->getName(), "Lobby") ||
//			0 == strcmpi(pkg->getName(), "Bank"))
		if ((pkg->getName() == "Lobby") ||
			(pkg->getName() == "Bank"))
		{
			PatchServer::This->unlockPackageList();
			pipe->sendError(APE_INVALID_REMOVE);
			return;
		}

		if (list->removeItem(pkg))
		{
			delete pkg;

			list->save();

			pipe->msgStart();
			pipe->msgAdd('S');
			pipe->msgEnd();
		}
		else
			pipe->sendError(APE_MEMORY_ERROR);

		PatchServer::This->unlockPackageList();
	}
	else
		pipe->sendError(APE_UNKNOWN_REQUEST);
}
